package com.hzzy.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hzzy.bean.BaseInfo;
import com.hzzy.bean.HzClasses;
import com.hzzy.bean.HzDormClassSummary;
import com.hzzy.bean.HzDormitorRecord;
import com.hzzy.bean.HzSemester;
import com.hzzy.bean.HzStudent;
import com.hzzy.bean.HzStudentContact;
import com.hzzy.enums.UserSessionKeyEnum;
import com.hzzy.service.HzClassesService;
import com.hzzy.service.HzCommonService;
import com.hzzy.service.HzSemesterService;

@Controller
@RequestMapping("/query")
public class QueryController {
	@Resource
	private HzCommonService hzCommonService;
	@Resource
	private HzClassesService hzClassesService;
	@Resource
	private HzSemesterService hzSemesterService;
	
	
	/**
	 * 通过学生id获取学生信息详情
	 */
	@RequestMapping("/studentInfo")
	public String querystudentinfobyid(HttpServletRequest request, Model model){
		
		String sid = request.getParameter("studentid");
		Map<String, Object> param = new HashMap();
		param.put("studentid", sid);
		HzStudent stuent= hzCommonService.queryStudentByStudentid(param);
		List<HzStudentContact> slist = new ArrayList<HzStudentContact>();
		HzStudentContact tempmy=new HzStudentContact();
		HzStudentContact tempf=new HzStudentContact();
		HzStudentContact tempm=new HzStudentContact();
		HzStudentContact tempt=new HzStudentContact();
		if(stuent==null){
			model.addAttribute("slist", slist);
			return "studentinfo.page";	
		}
		tempmy.setName(stuent.getName());
		tempmy.setPhone(stuent.getPhone());
		tempmy.setRelation("自己");
		tempf.setName(stuent.getFatherName());
		tempf.setPhone(stuent.getFatherPhone());
		tempf.setRelation("父亲");
		tempm.setName(stuent.getMotherName());
		tempm.setPhone(stuent.getMotherPhone());
		tempt.setRelation("班主任");
		tempt.setName(stuent.getTeacherName());
		tempt.setPhone(stuent.getTeacherPhone());
		slist.add(tempmy);
		slist.add(tempf);
		slist.add(tempm);
		slist.add(tempt);
		
		
		
		model.addAttribute("slist", slist);
		return "studentinfo.page";
	}
	/**
	 * 通过老师获取管辖的学生寝室点到记录,以天分隔
	 */
	@RequestMapping("/dormTCheckDormDRecord")
	public String querydormDbyteacherid(HttpServletRequest request, Model model){
		BaseInfo baseInfo  =(BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String time = df.format(new Date());
		String teacherid = request.getParameter("teacherid");
		String date = request.getParameter("date");
		Map<String, Object> param = new HashMap();
		teacherid = baseInfo.getTeacher().getTeacherId();
		param.put("teacherid", teacherid==null||"".equals(teacherid)?"0":teacherid);
		String timep=date==null||"".equals(date)?time:date;
		param.put("date", timep);
		List<HzDormitorRecord> drlist= hzCommonService.querydormmembersByDTeacherid(param);
		model.addAttribute("drlist", drlist);
		model.addAttribute("title", "我管理的寝室");
		model.addAttribute("url", "dormTCheckDormDRecord");
		model.addAttribute("date",timep);
		return "dormitorycrday.page";
	}
	/**
	 * 班主任通过自己,以天分隔
	 */
	@RequestMapping("/dormTCheckDormDRecordC")
	public String querydormDbyCteacherid(HttpServletRequest request, Model model){
		BaseInfo baseInfo  =(BaseInfo) request.getSession().getAttribute(UserSessionKeyEnum.BASE_USER_INFO.getValue());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String time = df.format(new Date());
		String teacherid = request.getParameter("teacherid");
		String date = request.getParameter("date");
		Map<String, Object> param = new HashMap();
		teacherid = baseInfo.getTeacher().getTeacherId();
		param.put("teacherid", teacherid==null||"".equals(teacherid)?"0":teacherid);
		String timep=date==null||"".equals(date)?time:date;
		param.put("date", timep);
		List<HzDormitorRecord> drlist= hzCommonService.querydormmembersByCTeacherid(param);
		model.addAttribute("drlist", drlist);
		model.addAttribute("title", "我管理的班级");
		model.addAttribute("url", "dormTCheckDormDRecordC");
		model.addAttribute("date",timep);
		return "dormitorycrday.page";
	}
	/**
	 * 通过老师获取管辖的学生寝室点到概述,以学期分隔(超级管理员)
	 */
	@RequestMapping("/dormTCheckDormYRecord")
	public String querydormYbyadmin(HttpServletRequest request, Model model){
		return "dormitorycryear.page";
	}
	/**
	 * 通过老师获取管辖的学生寝室点到概述,以学期分隔(超级管理员)(数据)
	 */
	@ResponseBody
	@RequestMapping("/dormTCheckDormYRecorddata")
	public Map querydormYbyadmindata(HttpServletRequest request, Model model){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		df.format(new Date());
		Map<String,Object> result =new HashMap<String, Object>();
		List<HzDormClassSummary> hlist =new ArrayList<HzDormClassSummary>(); 
		List<HzClasses> clist= hzClassesService.queryAllClass();
		int semesterid= Integer.valueOf(request.getParameter("semesterid"));
		HzSemester semester= hzSemesterService.selectSemesterBySemesterId(semesterid);
		for (HzClasses temp: clist){
			String classid= temp.getId().toString();
			HzDormClassSummary htemp = new HzDormClassSummary();
			htemp.setClassName(temp.getClassName());
			htemp.setVanishnum(hzCommonService.queryClassDormCheckinYear(classid, df.format(semester.getStartt()),df.format(semester.getEndt()), 1));
			htemp.setLeavenum(hzCommonService.queryClassDormCheckinYear(classid, df.format(semester.getStartt()), df.format(semester.getEndt()), 2));
			htemp.setNotcheck(hzCommonService.queryClassDormCheckinYear(classid, df.format(semester.getStartt()), df.format(semester.getEndt()), 3));
			htemp.setClassid(classid);
			hlist.add(htemp);
		}
		result.put("hlist", hlist);
		return result;
	}
	@ResponseBody
	@RequestMapping("/queryAllSemester")
	public Map queryAllSemester(){
		Map<String,Object> result =new HashMap<String, Object>();
		List<HzSemester> slist=hzSemesterService.selectAllSemester();
		result.put("slist", slist);
		return result;
	}
	
	
	
	
	/**
	 * 管理员通过班级id和学期id获取该班级学生学期寝室点到的详情(超级管理员)
	 */
	@RequestMapping("/dormTCheckDormYCDRecord")
	public String querydormYbysidandcid(HttpServletRequest request, Model model){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		df.format(new Date());
		int semesterid= Integer.valueOf(request.getParameter("semesterid"));
		String classid= request.getParameter("classid");
		HzSemester semester= hzSemesterService.selectSemesterBySemesterId(semesterid);
		List<HzDormitorRecord> drlist= hzCommonService.queryClassCheckinbycid(classid, df.format(semester.getStartt()), df.format(semester.getEndt()));
		model.addAttribute("drlist", drlist);
		String titletemp="";
		if(drlist.size()>0){
			titletemp=drlist.get(0).getClassName();
		}
		model.addAttribute("title", titletemp);
	
		return "dormitorycrydetails.page";
	}

	
	
	

}
